#coding: utf-8
from __future__ import with_statement
from fabric.api import *
from fabric import context_managers
from voa_utilities import *

'''
conf_wrapper needs further adjustment to check for shell startup files on the target system. See the following info from the OS X man page for startup file processing ...

When Bash is invoked as an interactive login shell, or as a non-interactive shell with the ‘--login’ option, it first reads and executes commands from the file ‘/etc/profile’, if that file exists. After reading that file, it looks for ‘~/.bash_profile’, ‘~/.bash_login’, and ‘~/.profile’, in that order, and reads and executes commands from the first one that exists and is readable. The ‘--noprofile’ option may be used when the shell is started to inhibit this behavior.
When a login shell exits, Bash reads and executes commands from the file ‘~/.bash_logout’, if it exists.
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
'''

def install_virtualenvwrapper():
    '''
    1 Install virtualenvwrapper to host os
    $ pip install virtualenvwrapper
    '''
    sudo('pip install virtualenvwrapper')

def export_workon_home():
    '''
    Step 2 
    $ export WORKON_HOME=~/Envs
    '''
    run('echo "export WORKON_HOME=%(workon_path)s" % env')
    run('echo $WORKON_HOME')

def create_workon_home():
    '''
    # 3     Create working home dir for virtualenvwrapper
    $ mkdir -p $WORKON_HOME
    '''
    run('mkdir -p %(workon_path)s' % env)


def source_virtualenvwrapper():
    '''
    Step 4
    $ source /usr/local/bin/virtualenvwrapper.sh
    '''
    setup_wrapper_paths()
    run('source %(wrapper_location)s' % env)

def setup_wrapper_paths():
    '''
    Find the virtualenvwrapper or virtualenvwrapper.sh script location
    '''
    with settings(warn_only=True):
        env.wrapper_location = run('which virtualenvwrapper')
    if env.wrapper_location.endswith('virtualenvwrapper'):
        run('echo "found virtualenvwrapper"')
    else:
        env.wrapper_location = run('which virtualenvwrapper.sh')
        run('echo "ignore the previous error"')
        run('echo "found virtualenvwrapper.sh"')


def conf_wrapper():
    '''
    check for the presence of env.workon_home in the users
    env.profile if it is not present then we find the location of
    virtualwrapper or virtualwrapper.sh and save the path to
    the corresponding text file.
    '''
    setup_wrapper_paths()
    current_profile =   run('cat ~/%s' % env.profile)
    if env.workon_path in current_profile:
        print('paths are already in %s' % env.profile)
    else:
        code = \
"""
# start voa-fab installer append" > bash_profile')
# IMPORTANT set \$PATH before source for virtualenvwrapper.sh
if [ \$USER == %(user)s ]; then
  export WORKON_HOME=%(workon_path)s
  source %(wrapper_location)s
fi
# end voa-fab installer append
""" % env
        tempfile = 'tempfile~'
        run('touch %s' % tempfile)
        for line in code.splitlines():
            run('echo "%s" >> %s' % (line, tempfile))
        run('cat %s >> ~/%s' % (tempfile, env.profile))
        run('rm %s' % tempfile)
 
